using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using NBear.Test.temp.Scope_OneTableOneExtendedTable_RefactoredWithInterfaceContract;
using NBear.Common;
using NBear.Data;

namespace NBear.Test.UnitTests
{
    namespace _Entity
    {
        public class AgentUser
        {
            public static PropertyItem LoginId = new PropertyItem("LoginId");
            public static PropertyItem Id = new PropertyItem("Id");
            public static PropertyItem Name = new PropertyItem("Name");
            public static PropertyItem PrivilegeOwnerId = new PropertyItem("PrivilegeOwnerId");
        }

        public class GhostUser
        {
            public static PropertyItem Id = new PropertyItem("Id");
            public static PropertyItem Name = new PropertyItem("Name");
            public static PropertyItem PrivilegeOwnerId = new PropertyItem("PrivilegeOwnerId");
        }

        public class IdentableEntity
        {
            public static PropertyItem Id = new PropertyItem("Id");
            public static PropertyItem Name = new PropertyItem("Name");
        }

        public class IdFactory
        {
            public static PropertyItem NextId = new PropertyItem("NextId");
        }

        public class LocalUser
        {
            public static PropertyItem Password = new PropertyItem("Password");
            public static PropertyItem LoginId = new PropertyItem("LoginId");
            public static PropertyItem Id = new PropertyItem("Id");
            public static PropertyItem Name = new PropertyItem("Name");
            public static PropertyItem PrivilegeOwnerId = new PropertyItem("PrivilegeOwnerId");
        }

        public class Loginable
        {
            public static PropertyItem LoginId = new PropertyItem("LoginId");
        }

        public class PasswordLoginable
        {
            public static PropertyItem Password = new PropertyItem("Password");
            public static PropertyItem LoginId = new PropertyItem("LoginId");
        }

        public class PrivilegeAssignable
        {
            public static PropertyItem PrivilegeOwnerId = new PropertyItem("PrivilegeOwnerId");
        }

        public class User
        {
            public static PropertyItem Id = new PropertyItem("Id");
            public static PropertyItem Name = new PropertyItem("Name");
            public static PropertyItem PrivilegeOwnerId = new PropertyItem("PrivilegeOwnerId");
        }

        public class UserGroup
        {
            public static PropertyItem Comment = new PropertyItem("Comment");
            public static PropertyItem Id = new PropertyItem("Id");
            public static PropertyItem Name = new PropertyItem("Name");
            public static PropertyItem PrivilegeOwnerId = new PropertyItem("PrivilegeOwnerId");
        }

    }

    /// <summary>
    /// Summary description for TestInheritenceOneEntityOneExtendedTable
    /// </summary>
    [TestClass]
    public class TestInheritenceOneEntityOneExtendedTable
    {
        private static IdFactoryInt32 idFac;
        private static Gateway gateway;

        public TestInheritenceOneEntityOneExtendedTable()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext) 
        {
            gateway = new Gateway(DatabaseType.MsAccess, @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Teddy\NBear\src\NBear.Test\UnitTests\db\TestInheritence.mdb");
            //gateway = new Gateway(DatabaseType.SqlServer, "Server=(local);Database=tempdb;Uid=sa;Pwd=sa");

            idFac = new IdFactoryInt32(gateway, "IdFactory", "NextId");
        }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void TestOneEntityOneExtendedTableMapping()
        {
            ActiveEntity<LocalUser> user1 = new ActiveEntity<LocalUser>(gateway);
            user1.Entity.Id = idFac.GetNextId();
            user1.Entity.LoginId = "teddy";
            user1.Entity.Name = "teddy";
            user1.Entity.Password = "teddy";
            user1.Entity.PrivilegeOwnerId = idFac.GetNextId();
            user1.Save();

            user1.Entity.Password = "modified teddy";
            user1.Save();

            ActiveEntity<User> user2 = new ActiveEntity<User>(gateway, user1.Entity.Id);
            user2.Delete();

            Assert.IsFalse(gateway.Exists<User>(user1.Entity.Id));

            LocalUser[] users = gateway.Select<LocalUser>(_Entity.LocalUser.Id > 5 | _Entity.LocalUser.LoginId == "teddy", _Entity.LocalUser.Id.Desc & _Entity.LocalUser.LoginId.Asc);
        }
    }
}
